package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member findByTel(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberMapper.add(member);
    }

    @Override
    public List<Integer> getMemberReport(List<String> months) {
        List<Integer> list = new ArrayList<>();
        String beforeMonth;
        String afterMonth;
        for (String month : months) {
            beforeMonth = month + ".31";
            afterMonth = month + ".01";
            //统计在当前月31号之前有多少会员
            Integer countBeforeDate = memberMapper.findMemberCountBeforeDate(beforeMonth);
            //统计在当前月1号之前有多少会员
            Integer countAfterDate = memberMapper.findMemberCountBeforeDate(afterMonth);
            //向统计每个月对应会员数的集合中添加数据
            list.add(countBeforeDate-countAfterDate);
        }
        return list;
    }
}
